<template>
  <div class="home-wrap">
    <div class="home-con">
      <div class="show-title">
        <span>未分配的设备</span>
      </div>
      <!-- 搜索 -->
      <div class="search-box">
        <el-input
          :placeholder="placeholder"
          v-model="input"
          class="input-with-select"
        >
          <el-select
            v-model="select"
            slot="prepend"
            style="width: 150px;"
            placeholder="请选择"
          >
            <el-option label="设备别名" value="1"></el-option>
          </el-select>
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="fnSearch"
          ></el-button>
          <i
            slot="suffix"
            class="el-input__icon el-icon-circle-close"
            @click="FnAll()"
          ></i>
        </el-input>
      </div>
      <!-- 列表 -->
      <el-table
        ref="table"
        :data="tableData"
        border
        style="width: 100%"
        stripe
        @selection-change="handleSelectionChange"
        @sort-change="changeSort"
        :row-key="getRowKeys"
      >
        <el-table-column
          type="selection"
          min-width="40"
          align="center"
          :reserve-selection="true"
          fixed
        ></el-table-column>
        <el-table-column
          label="设备标识ID"
          prop="device_sn"
          min-width="380"
          align="center"
        >
          <template slot-scope="scope">
            <div>{{ scope.row.device_sn }}</div>
          </template>
        </el-table-column>
        <el-table-column
          label="设备国标编码"
          prop="sn_gb28181"
          min-width="190"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.sn_gb28181?scope.row.sn_gb28181:"-" }}</template>
        </el-table-column>
        <el-table-column
          label="设备别名"
          prop="alias"
          min-width="280"
          align="center"
        >
          <template slot-scope="scope">
            {{ scope.row.alias }}
          </template>
        </el-table-column>
        <el-table-column
          label="主源地址"
          prop="stream_from"
          min-width="250"
          align="center"
        >
          <template slot-scope="scope">
            {{ scope.row.stream_from }}
          </template>
        </el-table-column>
        
        <el-table-column
          class="play-box"
          label="操作"
          min-width="140"
          align="center"
          fixed="right"
          v-if="permissionData.hideActionArr.length < 1"
        >
          <template slot-scope="scope">
            <el-button
              size="small"
              type="primary"
              @click="distributeFn(scope.$index, scope.row)"
              v-permissions="{
                url: ['host/device/add'],
                index: scope.$index,
                key: 'hideAction',
                id: '分配',
              }"
              >分配主机</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <el-pagination
        right
        background
        layout="total,prev, pager, next, sizes,jumper"
        :page-sizes="pageSizesArr"
        :page-size="pagesize"
        :total="total"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      ></el-pagination>
      <!--  批量-->
      <div
        class="batch-btn-con"
        v-permissions="{
          url: ['host/device/add'],
          hasDisplay: true,
        }"
      >
        <el-form
          :inline="true"
          :model="ruleform"
          ref="form_switch"
          :rules="rules"
        >
          <div class="con">
            <el-form-item label="分配至主机" prop="host_sn">
              <el-select v-model="ruleform.host_sn" placeholder="请选择">
                <el-option
                  v-for="item in hosts_arr"
                  :key="item"
                  :label="item"
                  :value="item"
                />
              </el-select>
            </el-form-item>
            <el-button
              class="batchDelect"
              type="success"
              plain
              @click="batchDistribute('form_switch')"
              >批量分配主机</el-button
            >
          </div>
        </el-form>
      </div>
    </div>
    <div
      class="home-con"
      v-permissions="{
        url: ['device/register'],
        hasDisplay: true,
      }"
    >
    </div>
    <!-- 点击分配，弹出框 -->
    <DistributeHost
      :dialogVisible="dialogVisible"
      :sn="sn"
      v-if="dialogVisible.show"
      :device_sn="device_sn"
      :alias="alias"
      @refreshList="refresh"
    />
  </div>
</template>

<script>
import { getData, postData } from "@/request/http";
import DistributeHost from "./DistributeHostPopup.vue";

export default {
  name: "list",
  components: { DistributeHost },
  props: {},
  data() {
    return {
      placeholder: "", //搜索中placeholder
      select: "", //搜索中select
      input: "", //搜索中input
      currpage: 1, //当前页
      pagesize: globalPageSizes[0], //初始显示，一页几个
      pageSizesArr: globalPageSizes, //globalPageSizes在public--config中全局配置，每页显示个数数组
      total: 0, // 总数
      tableData: [], // 表格数据
      tableChecked: [], // 多选
      batchDelectId: [], //批量删除id
      serialNum: "",
      checkAll: false, // 是否全选状态
      delBtnShow: false,
      ruleform: {
        host_sn: "",
      },
      rules: {
        host_sn: [{ required: false, message: "", trigger: "blur" }],
      },
      hosts_arr: [],
      //弹出框
      dialogVisible: {
        show: false,
      },
      sn: "", //父组件传递来的主机序列号
      device_sn: "",
      permissionData: {
        hideActionArr: [], // 操作按钮的隐藏
        hideURLArr: [], //有票无票的隐藏
      },
    };
  },
  computed: {},
  created() {},
  mounted() {
    this.sn = this.$route.query.sn;
    this.initShow();
    this.search();
    if (this.select == 1) {
      this.placeholder = "请输入名称";
    } else if (this.select == 2) {
      this.placeholder = "请输入国标编码";
    } else if (this.select == 3) {
      this.placeholder = "请输入序列号";
    } else {
      this.placeholder = "请输入";
    }
  },
  methods: {
    // 搜索
    async fnSearch() {
      let { input, select } = this;
      this.currpage = 1;

      if (input == "") {
        this.$message.error("请输入内容");
        return;
      }
      this.search();
    },
    search() {
      let dataParams = {};
      if (this.select == 1) {
        dataParams = {
          page: this.currpage,
          page_num: this.pagesize,
          alias: this.input,
        };
      }  else {
        dataParams = {
          page: this.currpage,
          page_num: this.pagesize,
        };
      }
      postData("/adminweb/host/device/list/notdistributed", dataParams)
        .then((res) => {
          if (res.code == 0) {
            if (!res.datas || !res.datas.devices) {
              return;
            }
            let devices = res.datas.devices;
            this.tableData = devices;
            this.total = res.datas.total_count;
          } else if (res.code == -56055) {
            return;
          } else {
            this.$message.error(res.code + "，" + res.msg);
          }
        })
        .catch((res) => {
          this.$message.error(res.code + "，" + res.msg);
        });
    },
    //全部
    FnAll() {
      this.currpage = 1;
      this.input = "";
      this.select = "";
      this.search();
    },
    // 获取row的key值
    getRowKeys(row) {
      return row.sn;
    },
    //点击 第几页
    handleCurrentChange(cpage) {
      this.currpage = cpage;
      this.search();
    },
    //每页有几条
    handleSizeChange(psize) {
      this.currpage = 1;
      this.pagesize = psize;
      this.search();
    },
    //操作多选
    handleSelectionChange(val) {
      var that = this;
      this.batchDelectId = [];
      that.tableChecked = val;
      //  console.log(that.tableChecked);
      if (val.length > 0) {
        for (let i = 0; i < val.length; i++) {
          this.batchDelectId.push(val[i]["sn"]);
        }
      }
    },
    //排序
    changeSort(val) {
      //val.order = ascending上升    descending下降
      //   console.log(val);
    },
    
    //点击分配主机按钮，弹窗
    distributeFn(index, row) {
      this.dialogVisible = { show: true };
      this.device_sn = row.device_sn;
      this.alias = row.alias;
    },
    refresh() {
      this.search();
    },

    //获取主机列表下拉框,
    initShow() {
      postData("/adminweb/host/list", {
        host_type: 2,
        io_type: [0, 1],
        status: 1,
      })
        .then((res) => {
          // console.log(res);
          if (res.code == 0) {
            let hosts = res.datas.hosts;
            var hosts_arr = [];
            for (var i = 0; i < hosts.length; i++) {
              hosts_arr.push(hosts[i].sn);
            }
            this.hosts_arr = hosts_arr;
          } else if (res.code == -56055) {
            return;
          } else {
            this.$message.error(res.code + "，" + res.msg);
          }
        })
        .catch((res) => {
          //this.$message.error(res.code + "，" + res.msg);
        });
    },
    //批量切换
    batchDistribute(formName) {
      //console.log(this.device_sn);
      var that = this;
      var device_sn_arr = [];
      device_sn_arr.push(that.device_sn);
      // console.log(device_sn_arr);
      that.$refs[formName].validate((valid) => {
        if (valid) {
          postData("/adminweb/host/device/add", {
            sn: that.ruleform.host_sn,
            devices: that.batchDelectId,
          })
            .then((res) => {
              if (res.code == 0) {
                that.$message.success("切换成功！");
                //刷新外部table的接口
                 that.search();
              } else {
                that.$message.error(res.code + "，" + res.msg);
              }
            })
            .catch((res) => {
              that.$message.error(res.code + "，" + res.msg);
            });
        } else {
          that.$message.error("切换失败");
          return false;
        }
      });
    },
  },
  watch: {
    //添加组件添加完刷新table
    submitData(newVal, oldVal) {
      if (newVal != oldVal) {
        this.search();
        this.currpage = 1;
      }
    },
  },
};
</script>
<style scoped>
.home-wrap {
}
.home-con {
  background: #fff;
  padding: 10px;
  margin-bottom: 30px;
  overflow: hidden;
}
.home-wrap >>> .el-pagination {
  float: right;
  margin-top: 30px;
}
.searchBox {
  margin-bottom: 20px;
}

/*标题*/
.show-title {
  width: 100%;
  height: 40px;
  line-height: 40px;
  position: relative;
  background: #c0dee2;
  padding-left: 15px;
  font-size: 14px;
  margin: 6px 0 20px 0;
  border-radius: 10px 10px 0 0;
  font-weight: bold;
}

.show-title::before {
  content: "";
  width: 2px;
  height: 16px;
  background: #676a6d;
  position: absolute;
  top: 12px;
  left: 27px;
}

.show-title span {
  float: left;
  color: #676a6d;
  margin-left: 25px;
}
.show-title .manual-refresh {
  float: right;
  margin-right: 20px;
}
.con {
  margin-top: 70px;
}
/* ip列表 */
.cameraList {
  margin-top: 35px;
  /* padding: 0 20px; */
}

/* 更改form表单默认样式,表头 */
.home-wrap >>> .el-table--border th,
.el-table__fixed-right-patch {
  /* background: #c0dee2; */
  background: #f0f3f4;
}

.home-wrap >>> .el-table .cell,
.el-table--border td:first-child .cell,
.el-table--border th:first-child .cell {
  padding-left: 10px;
  text-align: center;
}
.home-wrap >>> .demo-form-inline {
  width: 96%;
  margin: 0 auto;
  margin-top: 35px;
  /* padding: 20px; */
  box-sizing: border-box;
  /* border: 1px solid #c0dee2;
  box-shadow: 0px 3px 1px #ceece8;
  border-radius: 25px; */
}
.ip-del-btn {
  color: #f13636;
}

/* 分页*/
.block {
  margin: 20px 0 0 10px;
}

.home-wrap >>> .el-icon-video-play {
  font-size: 20px !important;
  z-index: 990;
}
.home-wrap >>> .el-link--inner {
  font-size: 12px;
}

.home-wrap >>> .tips {
  margin-top: 10px;
  display: block;
  font-size: 18px;
}
.batch-btn-con {
  background: #fff;
  margin-top: 70px;
}

.upload {
  margin-left: 10px;
}
/* title显示名称id */
.home-wrap >>> .table-wrap {
  border: none;
  box-shadow: none;
}
.home-wrap >>> .el-divider__text.is-center {
  font-size: 17px;
}
.home-wrap >>> .el-divider__text {
  background: #c0dee2;
  padding: 15px;
  border-radius: 8px;
}
.title-list {
  background: #fff;
  padding-left: 20px;
  height: 80px;
}
.title-list .txt {
  height: 10px;
}
.search-box {
  margin: 15px 0;
  width: 50%;
}
.batch-btn-con {
  background: #fff;
  margin-top: 50px;
  margin-bottom: 30px;
  padding-left: 20px;
}
</style>
